package com.zjut.zs.confirm.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zjut.an.util.module.PageModule;
import com.zjut.an.util.tools.FileDownload;
import com.zjut.an.util.tools.JSONUtilClass;
import com.zjut.zs.applyview.dao.ZsApplyView;
import com.zjut.zs.applyview.manager.ZsApplyViewManager;
import com.zjut.zs.confirm.dao.ZsConfirm;
import com.zjut.zs.confirm.manager.ZsConfirmManager;
import com.zjut.zs.user.dao.ZsUser;
import com.zjut.zs.user.manager.ZsUserManager;
import com.zjut.zs.userstatus.dao.ZsUserStatus;
import com.zjut.zs.xy.dao.ZsXy;
import com.zjut.zs.xy.manager.ZsXyManager;

public class ZsConfirmAction extends ActionSupport implements
ServletResponseAware, ServletRequestAware, ModelDriven<ZsConfirm>{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	
	private ZsConfirm obj;
	private Map<String , Object> dataMap;
	private int page = 1, limit = 10;
	private String order;
	private String sort;
	private String search;
	private String rowstr;

	@Resource(name = "ZsConfirmManager")
	private ZsConfirmManager manager;

	@Resource(name = "ZsUserManager")
	private ZsUserManager umanager;
	
	@Resource(name = "FileDownload")
	private FileDownload filedownload;
	
	@Resource(name = "ZsXyManager")
	private ZsXyManager xmanager;

	@Resource(name = "ZsApplyViewManager")
	private ZsApplyViewManager avManager;
	
	public String zsConfirmList() {
		try {
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			PageModule module = manager.getDataByInstance(page, limit, sort, order, obj);
			dataMap.put("rows", module.getObjects());
			dataMap.put("total", module.getTotal()); 
			obj = null; 
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	@SuppressWarnings("unchecked")
	public String zsConfirmDeletes() {
		try {
			String objumniJSON = rowstr;
			manager.deleteDatas(JSONUtilClass.getDTOList(objumniJSON, ZsConfirm.class));
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsConfirmAdds() {
		try {
			String objumniJSON = rowstr;
			List clist = JSONUtilClass.getDTOList(objumniJSON, ZsConfirm.class);
			for (int i=0; i<clist.size();i++) {
				ZsConfirm obj = (ZsConfirm)clist.get(i);
				ZsUser user = new ZsUser();
				user.setUsId(obj.getUsId());
				ZsXy xy = xmanager.getDataByUser(user);
				System.out.println("######################## "+xy.getXyId()  +xy.getXyYw() + obj.getCYw());
				
				xy.setXyYw(obj.getCYw());
				xy.setXyDl(obj.getCDl());
				xy.setXyHx(obj.getCHx());
				xy.setXyJs(obj.getCJs());
				xy.setXyLs(obj.getCLs());
				xy.setXySw(obj.getCSw());
				xy.setXySx(obj.getCSx());
				xy.setXyWl(obj.getCWl());
				xy.setXyWy(obj.getCWy());
				xmanager.updateData(xy);

				obj.setXyYw(obj.getCYw());
				obj.setXyDl(obj.getCDl());
				obj.setXyHx(obj.getCHx());
				obj.setXyJs(obj.getCJs());
				obj.setXyLs(obj.getCLs());
				obj.setXySw(obj.getCSw());
				obj.setXySx(obj.getCSx());
				obj.setXyWl(obj.getCWl());
				obj.setXyWy(obj.getCWy());
				manager.updateData(obj);

				System.out.println("######################## " +xy.getXyYw());
			}
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String zsCheckConfirm() {
		/*
		 * response.setContentType("text/html; charset=UTF-8");
		 * System.out.println("开始"); dataMap = new HashMap<String, Object>(); String msg
		 * = "";
		 * 
		 * try { List<ZsConfirm> cList = manager.getDatas(); int num=0; int nullid=0;
		 * for (ZsConfirm confirm : cList) { if (!isEmpty(confirm.getUsIdcard()))
		 * {//身份证必须非空 ZsApplyView obj = new ZsApplyView();
		 * obj.setUsIdcard(confirm.getUsIdcard()); List list =
		 * avManager.getDataByInstance(1, 1, null, null, obj).getObjects(); if
		 * (list.size()>0) { obj = (ZsApplyView)list.get(0);
		 * 
		 * confirm.setUsId(obj.getUsId()); confirm.setXyYw(obj.getXyYw());
		 * confirm.setXyWy(obj.getXyWy()); confirm.setXySx(obj.getXySx());
		 * confirm.setXyZz(obj.getXyZz()); confirm.setXyLs(obj.getXyLs());
		 * confirm.setXyDl(obj.getXyDl()); confirm.setXyWl(obj.getXyWl());
		 * confirm.setXyHx(obj.getXyHx()); confirm.setXySw(obj.getXySw());
		 * confirm.setXyJs(obj.getXyJs()); confirm.setXyZxzh1(obj.getXyZxzh1());
		 * confirm.setXyZxzh2(obj.getXyZxzh2()); confirm.setXyZxzh3(obj.getXyZxzh3());
		 * confirm.setXyZxzh4(obj.getXyZxzh4()); confirm.setCheckStatus("一致");
		 * if(!confirm.getCYw().equals(confirm.getXyYw())
		 * ||!confirm.getCWy().equals(confirm.getXyWy())
		 * ||!confirm.getCSx().equals(confirm.getXySx())
		 * ||!confirm.getCZz().equals(confirm.getXyZz())
		 * ||!confirm.getCLs().equals(confirm.getXyLs())
		 * ||!confirm.getCDl().equals(confirm.getXyDl())
		 * ||!confirm.getCWl().equals(confirm.getXyWl())
		 * ||!confirm.getCHx().equals(confirm.getXyHx())
		 * ||!confirm.getCSw().equals(confirm.getXySw())
		 * ||!confirm.getCJs().equals(confirm.getXyJs())
		 * ||!confirm.getCZxzh1().equals(confirm.getXyZxzh1())
		 * ||!confirm.getCZxzh2().equals(confirm.getXyZxzh2())
		 * ||!confirm.getCZxzh3().equals(confirm.getXyZxzh3())
		 * ||!confirm.getCZxzh4().equals(confirm.getXyZxzh4()) ) {
		 * confirm.setCheckStatus("否"); } manager.updateData(confirm); num++;
		 * System.out.println("身份证："+confirm.getUsIdcard()+" 匹配完成"); } else {
		 * System.out.println("身份证："+confirm.getUsIdcard()+" 查找无数据"); } }else{
		 * System.out.println("身份证："+confirm.getUsIdcard()+" 空"); nullid++;//身份证号码为空 } }
		 * 
		 * dataMap.put("flag", "success"); } catch (Exception e) { e.printStackTrace();
		 * msg = "excel 字段格式有误，系统无法转换"; dataMap.put("flag", "error"); dataMap.put("msg",
		 * msg); return ERROR; }
		 */
		return SUCCESS;
	}

	private boolean isEmpty(String value) {
		if (value==null || value.trim().length()==0) {
			return true;
		}
		return false;
	}
	
	public String zsConfirmDelete() {
		try {
			String objumniJSON = rowstr;
			//manager.deleteData((ZsConfirm)JSONUtilClass.getDTO(objumniJSON, ZsConfirm.class));
			manager.deleteData(manager.getData(Integer.parseInt(request.getParameter("zxId"))));
			dataMap = new HashMap<String, Object>();
			dataMap.put("flag", "success");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsConfirmUpdate() {
		try {
			String objumniJSON = rowstr;
			manager.updateData((ZsConfirm)JSONUtilClass.getDTO(objumniJSON, ZsConfirm.class));
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	public String zsConfirmSave() {
		obj = (ZsConfirm)(JSONUtilClass.getDTO(rowstr, ZsConfirm.class));
		if (obj.getId()!=null) {
			//-------update-------
			manager.updateData(obj);
		} else {
			//-------insert-------
			manager.addData(obj);
		}
		rowstr = null;
		return SUCCESS;
	}

	public String zsConfirm() {
		try {
			request.setCharacterEncoding("UTF-8");
			dataMap = new HashMap<String, Object>();
			Integer inId = Integer.parseInt(request.getParameter("inId"));
			obj = manager.getData(inId);
			dataMap.put("rows", obj);
			obj = null;
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}		
	}
	
	@Override
	public ZsConfirm getModel() {
		if (null == obj) {
			obj = new ZsConfirm();
		}
		return obj;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
		session = request.getSession();
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		response = arg0;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public ZsConfirm getAl() {
		return obj;
	}

	public void setAl(ZsConfirm obj) {
		this.obj = obj;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public String getRowstr() {
		return rowstr;
	}

	public void setRowstr(String rowstr) {
		this.rowstr = rowstr;
	}

	public ZsConfirmManager getManager() {
		return manager;
	}

	public void setManager(ZsConfirmManager manager) {
		this.manager = manager;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

}
